package com.yooyuu.app;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yooyuu.stomp.StompConfig;
import com.yooyuu.stomp.StompForApp;
import com.yooyuu.stomp.StompForAppCallBack;
import com.yooyuu.stomp.common.Ack;
import com.yooyuu.stomp.common.StompException;

import org.apache.cordova.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.net.URISyntaxException;

import static com.yooyuu.stomp.StompConfig.KEY_STATE;

public class Main2Activity extends Activity implements StompForAppCallBack.CallBackReceiver {
    private StompForApp stomp;
    StompForAppCallBack stompForAppCallBack;
    EditText editText;
    TextView textView;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText=(EditText)findViewById(R.id.editText);
        textView=(TextView)findViewById(R.id.textView);
        btn=(Button)findViewById(R.id.btn_send);
        String stompConfigStr="{\"headers\":{\"isConnected\":false,\"uri\":\"http:\\/\\/www.yooish.com:8080\\/MagicBox\\/magicboxSEP\",\"login\":\"admin@wr.cn\",\"passcode\":\"123456\",\"heart-beat\":\"10000,10000\",\"thirdPartyTypeId\":\"SELF\"}}]";
        JSONTokener jsonParser = new JSONTokener(stompConfigStr);//解释json串
        try {

            JSONObject json = (JSONObject) jsonParser.nextValue();
            final StompConfig config = StompConfig.fromJSON(json);
            stompForAppCallBack=new StompForAppCallBack("1",this);
            stompForAppCallBack.sendPluginResult(getPluginResult());
            stomp = new StompForApp( stompForAppCallBack, config);
            stomp.wsConnect();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (StompException e) {
            e.printStackTrace();
        }
        btn.setEnabled(false);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String body = "WRJW0044                  180001SYS00000SYS00000ComdGet     &device ";
                final String destination = "/exchange/magicbox.exchange/magicbox.00-3F-00-E1-76-29";
                try {
                    stomp.send(destination, body);
                } catch (StompException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    PluginResult getPluginResult() throws JSONException {
        JSONObject json = new JSONObject();

        PluginResult result = new PluginResult(PluginResult.Status.OK, json);
        result.setKeepCallback(true);

        return result;
    }
    @Override
    public void onDisconnect() {
        Log.w("Main2Activity", "onDisconnect" );
    }

    @Override
    public void onReceive(final PluginResult pluginResult) {
      this.runOnUiThread(new Runnable() {
            public void run() {


        JSONTokener jsonParser = new JSONTokener(pluginResult.getMessage());//解释json串
        {
            try {
                if(TextUtils.isEmpty(pluginResult.getMessage()))
                    return;
                JSONObject json = (JSONObject) jsonParser.nextValue();
                if(json.has(StompConfig.KEY_STATE))
                if(json.get(StompConfig.KEY_STATE).equals("connected"))
                {
                    try
                    {
                        stomp.subscribe("/topic/app.18", Ack.client);
                        btn.setEnabled(true);


                    }
                    catch (Exception e)
                    {
                        stompForAppCallBack.error(e.getMessage());
                    }

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
textView.setText(textView.getText().toString()+"\n"+pluginResult.getMessage());
        Log.d("Main2Activity", "Attempted to send a second callback Result was: " + pluginResult.getMessage());
            }
      });
    }
}
